﻿#ifndef SERIALIZATION_H
#define SERIALIZATION_H

#include <QJsonObject>
#include <QJsonDocument>
#include <QString>
#include <QDebug>
#include <QJsonArray>
#include "enumserialization.h"
#include "rtcdefine.h"

using namespace ding::rtc;


// 将 JSON 字符串转换为 RtcEngineAuthInfo 对象
static RtcEngineAuthInfo desRtcEngineAuthInfo(const QString& jsonString) {
    QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString.toUtf8());

    if (!jsonDoc.isObject()) {
        qWarning() << "Invalid JSON: Not an object!";
        return RtcEngineAuthInfo();
    }

    QJsonObject jsonObject = jsonDoc.object();
    RtcEngineAuthInfo info;

    info.channelId = jsonObject["channelId"].toString().toStdString().c_str();
    qDebug() << "Parsed channelId:" << info.channelId.c_str();

    info.userId = jsonObject["userId"].toString().toStdString().c_str();
    qDebug() << "Parsed userId:" << info.userId.c_str();

    info.appId = jsonObject["appId"].toString().toStdString().c_str();
    qDebug() << "Parsed appId:" << info.appId.c_str();

    info.token = jsonObject["token"].toString().toStdString().c_str();
    qDebug() << "Parsed token:" << info.token.c_str();

    info.gslbServer = jsonObject["gslbServer"].toString().toStdString().c_str();
    qDebug() << "Parsed gslbServer:" << info.gslbServer.c_str();

    return info;
}

static QJsonObject sRtcEngineDeviceInfo(const RtcEngineDeviceInfo& deviceInfo) {
    QJsonObject jsonObject;
    jsonObject["deviceName"] = QString::fromUtf8(deviceInfo.deviceName.c_str()); // 将 String 转换为 QString
    jsonObject["deviceID"] = QString::fromUtf8(deviceInfo.deviceID.c_str());
    return jsonObject;
}

// 序列化 RtcEngineDeviceInfoList
static QJsonObject sRtcEngineDeviceInfoList(RtcEngineDeviceInfoList* deviceInfoList) {
    QJsonObject jsonObject;
    QJsonArray devicesArray;

    // 遍历 RtcEngineDeviceInfoList 并将每个设备信息序列化
    unsigned int count = deviceInfoList->GetCount();
    for (unsigned int i = 0; i < count; ++i) {
        RtcEngineDeviceInfo deviceInfo = deviceInfoList->GetDeviceInfo(i);
        devicesArray.append(sRtcEngineDeviceInfo(deviceInfo));
    }

    // 将设备列表存入 JSON 对象
    jsonObject["devices"] = devicesArray;
    jsonObject["count"] = static_cast<int>(count);

    return jsonObject;
}

// 序列化 RtcEngineVideoDimensions
static QJsonObject sRtcEngineVideoDimensions(const RtcEngineVideoDimensions& dimensions) {
    QJsonObject jsonObject;
    jsonObject["width"] = dimensions.width;
    jsonObject["height"] = dimensions.height;
    return jsonObject;
}

// 反序列化 RtcEngineVideoDimensions
static RtcEngineVideoDimensions desRtcEngineVideoDimensions(const QJsonObject& jsonObject) {
    RtcEngineVideoDimensions dimensions;
    if (jsonObject.contains("width") && jsonObject["width"].isDouble()) {
        dimensions.width = jsonObject["width"].toInt();
    }
    if (jsonObject.contains("height") && jsonObject["height"].isDouble()) {
        dimensions.height = jsonObject["height"].toInt();
    }
    return dimensions;
}

// 序列化 RtcEngineVideoEncoderConfiguration
static QJsonObject sRtcEngineVideoEncoderConfiguration(const RtcEngineVideoEncoderConfiguration& config) {
    QJsonObject jsonObject;
    jsonObject["dimensions"] = sRtcEngineVideoDimensions(config.dimensions);
    jsonObject["frameRate"] = config.frameRate;
    jsonObject["orientationMode"] = static_cast<int>(config.orientationMode);
    jsonObject["rotation"] = config.rotation;
    return jsonObject;
}

// 反序列化 RtcEngineVideoEncoderConfiguration
static RtcEngineVideoEncoderConfiguration desRtcEngineVideoEncoderConfiguration(const QJsonObject& jsonObject) {
    RtcEngineVideoEncoderConfiguration config;

    if (jsonObject.contains("dimensions") && jsonObject["dimensions"].isObject()) {
        config.dimensions = desRtcEngineVideoDimensions(jsonObject["dimensions"].toObject());
    }
    if (jsonObject.contains("frameRate") && jsonObject["frameRate"].isDouble()) {
        config.frameRate = jsonObject["frameRate"].toInt();
    }
    if (jsonObject.contains("orientationMode") && jsonObject["orientationMode"].isDouble()) {
        config.orientationMode = static_cast<RtcEngineVideoEncoderOrientationMode>(jsonObject["orientationMode"].toInt());
    }
    if (jsonObject.contains("rotation") && jsonObject["rotation"].isDouble()) {
        config.rotation = jsonObject["rotation"].toInt();
    }

    return config;
}

// 序列化 RtcEngineVideoCanvas
static QJsonObject sRtcEngineVideoCanvas(const RtcEngineVideoCanvas& canvas) {
    QJsonObject jsonObject;
    jsonObject["displayView"] = reinterpret_cast<qint64>(canvas.displayView);
    jsonObject["backgroundColor"] = canvas.backgroundColor;
    jsonObject["renderMode"] = sRtcEngineRenderMode(canvas.renderMode);
    jsonObject["mirrorMode"] = sRtcEngineRenderMirrorMode(canvas.mirrorMode);
    jsonObject["scaleMode"] = sRtcEngineVideoScale(canvas.scaleMode);
    jsonObject["rotation"] = sRtcEngineRotationMode(canvas.rotation);
    jsonObject["renderId"] = static_cast<qint64>(canvas.renderId);
    jsonObject["width"] = canvas.width;
    jsonObject["height"] = canvas.height;
    jsonObject["enableGL"] = canvas.enableGL;
    jsonObject["toBeRemoved"] = canvas.toBeRemoved;
    jsonObject["enableHwAcceleration"] = canvas.enableHwAcceleration;

    return jsonObject;
}

// 反序列化 RtcEngineVideoCanvas
static RtcEngineVideoCanvas desRtcEngineVideoCanvas(const QJsonObject& jsonObject) {
    RtcEngineVideoCanvas canvas;

    qlonglong view = jsonObject["displayView"].toVariant().toLongLong();

    canvas.displayView = (view == 0 ? nullptr : reinterpret_cast<void*>(view));
    canvas.backgroundColor = jsonObject["backgroundColor"].toInt();
    canvas.renderMode = desRtcEngineRenderMode(jsonObject["renderMode"]);
    canvas.mirrorMode = desRtcEngineRenderMirrorMode(jsonObject["mirrorMode"]);
    canvas.scaleMode = desRtcEngineVideoScale(jsonObject["scaleMode"]);
    canvas.rotation = desRtcEngineRotationMode(jsonObject["rotation"]);
    canvas.renderId = jsonObject["renderId"].toVariant().toLongLong();
    canvas.width = jsonObject["width"].toInt();
    canvas.height = jsonObject["height"].toInt();
    canvas.enableGL = jsonObject["enableGL"].toBool();
    canvas.toBeRemoved = jsonObject["toBeRemoved"].toBool();
    canvas.enableHwAcceleration = jsonObject["enableHwAcceleration"].toBool();

    return canvas;
}


// 序列化 RtcEngineBeautyFaceOptions
static QJsonObject sRtcEngineBeautyFaceOptions(const RtcEngineBeautyFaceOptions& options) {
    QJsonObject jsonObject;
//    jsonObject["resourcePath"] = options.resourcePath ? QString(options.resourcePath) : "";
    jsonObject["enableSkinBuffing"] = options.enableSkinBuffing;
    jsonObject["skinBuffingFactor"] = options.skinBuffingFactor;
    jsonObject["skinSharpenFactor"] = options.skinSharpenFactor;
    jsonObject["enableSkinWhitening"] = options.enableSkinWhitening;
    jsonObject["skinWhitingFactor"] = options.skinWhitingFactor;

    return jsonObject;
}

// 反序列化 RtcEngineBeautyFaceOptions
static RtcEngineBeautyFaceOptions desRtcEngineBeautyFaceOptions(const QJsonObject& jsonObject) {
    RtcEngineBeautyFaceOptions options;
//    options.resourcePath = jsonObject["resourcePath"].toString().isEmpty()
//                           ? "no path"
//                           : strdup(jsonObject["resourcePath"].toString().toUtf8().data());

    options.resourcePath= "no path";
    options.enableSkinBuffing = jsonObject["enableSkinBuffing"].toBool();
    options.skinBuffingFactor = jsonObject["skinBuffingFactor"].toDouble();
    options.skinSharpenFactor = jsonObject["skinSharpenFactor"].toDouble();
    options.enableSkinWhitening = jsonObject["enableSkinWhitening"].toBool();
    options.skinWhitingFactor = jsonObject["skinWhitingFactor"].toDouble();

    return options;
}


// 序列化 RtcEngineStats
static QJsonObject sRtcEngineStats(const RtcEngineStats& stats) {
    QJsonObject jsonObject;
    jsonObject["duration"] = static_cast<qint64>(stats.duration);
    jsonObject["txBytes"] = static_cast<qint64>(stats.txBytes);
    jsonObject["txAudioBytes"] = static_cast<qint64>(stats.txAudioBytes);
    jsonObject["txVideoBytes"] = static_cast<qint64>(stats.txVideoBytes);
    jsonObject["txKBitrate"] = stats.txKBitrate;
    jsonObject["txAudioKBitrate"] = stats.txAudioKBitrate;
    jsonObject["txVideoKBitrate"] = stats.txVideoKBitrate;
    jsonObject["txPacketLossRate"] = stats.txPacketLossRate;
    jsonObject["rxBytes"] = static_cast<qint64>(stats.rxBytes);
    jsonObject["rxPackets"] = static_cast<qint64>(stats.rxPackets);
    jsonObject["rxAudioBytes"] = static_cast<qint64>(stats.rxAudioBytes);
    jsonObject["rxVideoBytes"] = static_cast<qint64>(stats.rxVideoBytes);
    jsonObject["rxKBitrate"] = stats.rxKBitrate;
    jsonObject["rxAudioKBitrate"] = stats.rxAudioKBitrate;
    jsonObject["rxVideoKBitrate"] = stats.rxVideoKBitrate;
    jsonObject["rxPacketLossRate"] = stats.rxPacketLossRate;
    jsonObject["rxLostPackets"] = static_cast<qint64>(stats.rxLostPackets);
    jsonObject["lastmileDelay"] = stats.lastmileDelay;
    jsonObject["connectTimeMs"] = stats.connectTimeMs;
    jsonObject["systemCpuRate"] = stats.systemCpuRate;
    jsonObject["appCpuRate"] = stats.appCpuRate;
    return jsonObject;
}

// 序列化 QualityReport
static QJsonObject sQualityReport(const QualityReport& report) {
    QJsonObject jsonObject;
    jsonObject["cpu_physical_cores"] = report.cpu_physical_cores;
    jsonObject["cpu_logic_cores"] = report.cpu_logic_cores;
    jsonObject["cpu_frequency_mhz"] = report.cpu_frequency_mhz;
    jsonObject["cpu_used_by_app"] = report.cpu_used_by_app;
    jsonObject["cpu_used_by_other_apps"] = report.cpu_used_by_other_apps;
    jsonObject["memory_total_mb"] = report.memory_total_mb;
    jsonObject["memory_used_by_app"] = report.memory_used_by_app;
    jsonObject["memory_used_by_other_apps"] = report.memory_used_by_other_apps;
    jsonObject["loss_rate"] = report.loss_rate;
    jsonObject["rtt_ms"] = report.rtt_ms;
    jsonObject["recv_bitrate"] = report.recv_bitrate;
    jsonObject["send_bitrate"] = report.send_bitrate;
    jsonObject["signal_level"] = report.signal_level;
    jsonObject["network_type"] = QString::fromUtf8(report.network_type);
    jsonObject["audio_send_bitrate"] = report.audio_send_bitrate;
    jsonObject["audio_send_volume"] = report.audio_send_volume;
    jsonObject["audio_recv_bitrate"] = report.audio_recv_bitrate;
    jsonObject["audio_recv_volume"] = report.audio_recv_volume;
    jsonObject["camera_send_bitrate"] = report.camera_send_bitrate;
    jsonObject["camera_send_capture_fps"] = report.camera_send_capture_fps;
    jsonObject["camera_send_drop_fps"] = report.camera_send_drop_fps;
    jsonObject["camera_send_fps"] = report.camera_send_fps;
    jsonObject["camera_send_hw"] = report.camera_send_hw;
    jsonObject["camera_send_width"] = report.camera_send_width;
    jsonObject["camera_send_height"] = report.camera_send_height;
    jsonObject["screenshare_send_bitrate"] = report.screenshare_send_bitrate;
    jsonObject["screenshare_send_capture_fps"] = report.screenshare_send_capture_fps;
    jsonObject["screenshare_send_drop_fps"] = report.screenshare_send_drop_fps;
    jsonObject["screenshare_send_fps"] = report.screenshare_send_fps;
    jsonObject["screenshare_send_hw"] = report.screenshare_send_hw;
    jsonObject["screenshare_send_width"] = report.screenshare_send_width;
    jsonObject["screenshare_send_height"] = report.screenshare_send_height;
    jsonObject["camera_recv_bitrate"] = report.camera_recv_bitrate;
    jsonObject["camera_recv_fps"] = report.camera_recv_fps;
    jsonObject["camera_recv_render_fps"] = report.camera_recv_render_fps;
    jsonObject["camera_recv_hw"] = report.camera_recv_hw;
    jsonObject["camera_recv_width"] = report.camera_recv_width;
    jsonObject["camera_recv_height"] = report.camera_recv_height;
    jsonObject["screenshare_recv_bitrate"] = report.screenshare_recv_bitrate;
    jsonObject["screenshare_recv_fps"] = report.screenshare_recv_fps;
    jsonObject["screenshare_recv_render_fps"] = report.screenshare_recv_render_fps;
    jsonObject["screenshare_recv_hw"] = report.screenshare_recv_hw;
    jsonObject["screenshare_recv_width"] = report.screenshare_recv_width;
    jsonObject["screenshare_recv_height"] = report.screenshare_recv_height;
    return jsonObject;
}

// 序列化 LocalAudioStats
static QJsonObject sLocalAudioStats(const LocalAudioStats& stats) {
    QJsonObject jsonObject;
    jsonObject["track"] = static_cast<int>(stats.track); // 转为整数
    jsonObject["sentBitrate"] = static_cast<int>(stats.sentBitrate);
    jsonObject["sentSamplerate"] = static_cast<int>(stats.sentSamplerate);
    jsonObject["numChannel"] = static_cast<int>(stats.numChannel);
    jsonObject["inputLevel"] = static_cast<int>(stats.inputLevel);
    return jsonObject;
}

// 序列化 LocalVideoStats
static QJsonObject sLocalVideoStats(const LocalVideoStats& stats) {
    QJsonObject jsonObject;
    jsonObject["track"] = static_cast<int>(stats.track); // 转为整数
    jsonObject["targetEncodeBitrate"] = static_cast<int>(stats.targetEncodeBitrate);
    jsonObject["actualEncodeBitrate"] = static_cast<int>(stats.actualEncodeBitrate);
    jsonObject["sentBitrate"] = static_cast<int>(stats.sentBitrate);
    jsonObject["sentFps"] = static_cast<int>(stats.sentFps);
    jsonObject["encodeFps"] = static_cast<int>(stats.encodeFps);
    jsonObject["captureFps"] = static_cast<int>(stats.captureFps);
    jsonObject["renderFps"] = static_cast<int>(stats.renderFps);
    jsonObject["avgQpPerSec"] = static_cast<int>(stats.avgQpPerSec);
    jsonObject["encoderFrameWidth"] = stats.encoderFrameWidth;
    jsonObject["encoderFrameHeight"] = stats.encoderFrameHeight;
    jsonObject["captureFrameWidth"] = stats.captureFrameWidth;
    jsonObject["captureFrameHeight"] = stats.captureFrameHeight;
    return jsonObject;
}

// 序列化 RemoteAudioStats
static QJsonObject sRemoteAudioStats(const RemoteAudioStats& stats) {
    QJsonObject jsonObject;
    jsonObject["userId"] = QString::fromUtf8(stats.userId.c_str());
    jsonObject["packetLossRate"] = stats.packetLossRate;
    jsonObject["recvBitrate"] = stats.recvBitrate;
    jsonObject["totalFrozenTime"] = stats.totalFrozenTime;
    jsonObject["speechExpandRate"] = stats.speechExpandRate;
    return jsonObject;
}

// 序列化 RemoteVideoStats
static QJsonObject sRemoteVideoStats(const RemoteVideoStats& stats) {
    QJsonObject jsonObject;
    jsonObject["userId"] = QString::fromUtf8(stats.userId.c_str());
    jsonObject["track"] = stats.track;
    jsonObject["width"] = stats.width;
    jsonObject["height"] = stats.height;
    jsonObject["recvBitrate"] = stats.recvBitrate;
    jsonObject["decoderOutputFrameRate"] = stats.decoderOutputFrameRate;
    jsonObject["rendererOutputFrameRate"] = stats.rendererOutputFrameRate;
    jsonObject["packetLossRate"] = stats.packetLossRate;
    return jsonObject;
}

// 序列化 AudioVolumeInfo
static QJsonObject sAudioVolumeInfo(const AudioVolumeInfo& info) {
    QJsonObject jsonObject;
    jsonObject["userId"] = QString::fromUtf8(info.userId.c_str());
    jsonObject["volume"] = static_cast<int>(info.volume); // 转为整数
    jsonObject["speechState"] = static_cast<int>(info.speechState);
    return jsonObject;
}

// 序列化 RtcEngineAudioGroupMember
static QJsonObject sRtcEngineAudioGroupMember(const RtcEngineAudioGroupMember& member) {
    QJsonObject jsonObject;
    jsonObject["usrId"] = QString::fromUtf8(member.usrId.c_str());
    return jsonObject;
}

// 序列化 RtcEngineAudioMixingStatusConfig
static QJsonObject sRtcEngineAudioMixingStatusConfig(const RtcEngineAudioMixingStatusConfig& config) {
    QJsonObject jsonObject;
    jsonObject["status"] = sRtcEngineAudioMixingStatus(config.status);  // 调用枚举序列化函数
    jsonObject["errorCode"] = sRtcEngineAudioMixingErrorCode(config.errorCode);  // 调用枚举序列化函数
    jsonObject["fileName"] = QString::fromStdString(config.fileName);  // std::string 转 QString
    jsonObject["id"] = config.id;  // 文件 ID
    jsonObject["durationMs"] = static_cast<qint64>(config.durationMs);  // 将 long 转换为 qint64
    return jsonObject;
}

static QJsonObject sAudioVolumeInfoList(const std::vector<AudioVolumeInfo>& infos) {
    QJsonArray jsonArray;
    for (const auto& info : infos) {
        jsonArray.append(sAudioVolumeInfo(info)); // 将单个 AudioVolumeInfo 序列化后添加到数组
    }

    // 将 QJsonArray 嵌套到 QJsonObject 中
    QJsonObject jsonObject;
    jsonObject["audioVolumeInfoList"] = jsonArray; // 使用 "audioVolumeInfoList" 作为键

    return jsonObject; // 返回 QJsonObject
}

// 序列化 RtcEngineRecordResolution
static QJsonObject sRtcEngineRecordResolution(const RtcEngineRecordResolution& resolution) {
    QJsonObject jsonObject;
    jsonObject["width"] = static_cast<int>(resolution.width);
    jsonObject["height"] = static_cast<int>(resolution.height);
    return jsonObject;
}

// 反序列化 RtcEngineRecordResolution
static RtcEngineRecordResolution desRtcEngineRecordResolution(const QJsonObject& jsonObject) {
    RtcEngineRecordResolution resolution;
    if (jsonObject.contains("width")) {
        resolution.width = jsonObject["width"].toInt();
    } else {
        resolution.width = 1280; // 默认宽度
    }
    if (jsonObject.contains("height")) {
        resolution.height = jsonObject["height"].toInt();
    } else {
        resolution.height = 720; // 默认高度
    }
    return resolution;
}

// 序列化 RtcEngineRecordOptions
static QJsonObject sRtcEngineRecordOptions(const RtcEngineRecordOptions& options) {
    QJsonObject jsonObject;
    jsonObject["targetResolution"] = sRtcEngineRecordResolution(options.targetResolution);
    jsonObject["recordMode"] = sRtcEngineRecordMode(options.recordMode); // 使用 ENUM_SERIALIZATION 定义的序列化函数
    jsonObject["videoFps"] = options.videoFps;
    jsonObject["useHardwareAccelerate"] = options.useHardwareAccelerate;
    return jsonObject;
}

// 反序列化 RtcEngineRecordOptions
static RtcEngineRecordOptions desRtcEngineRecordOptions(const QJsonObject& jsonObject) {
    RtcEngineRecordOptions options;

    // 从参数中提取 targetResolution
    if (jsonObject.contains("targetResolution") && jsonObject["targetResolution"].isObject()) {
        options.targetResolution = desRtcEngineRecordResolution(jsonObject["targetResolution"].toObject());
    }

    // 从参数中提取 recordMode
    if (jsonObject.contains("recordMode")) {
        options.recordMode = desRtcEngineRecordMode(jsonObject["recordMode"]);
    }

    // 从参数中提取 videoFps
    if (jsonObject.contains("videoFps")) {
        options.videoFps = jsonObject["videoFps"].toInt();
    } else {
        options.videoFps = 20; // 默认帧率
    }

    // 从参数中提取 useHardwareAccelerate
    if (jsonObject.contains("useHardwareAccelerate")) {
        options.useHardwareAccelerate = jsonObject["useHardwareAccelerate"].toBool();
    } else {
        options.useHardwareAccelerate = false; // 默认不开启硬件加速
    }

    return options;
}

/**********************************************************************/

template<typename T>
QString sToJSONString(T object, QJsonObject (*sFunc)(T)) {
    if constexpr (std::is_pointer_v<T>) {  // 检查是否是指针
        if (!object) {
            return QString();  // 防止空指针引发问题
        }
    }
    QJsonObject jsonObject = sFunc(object);
    QJsonDocument jsonDoc(jsonObject);
    return QString::fromUtf8(jsonDoc.toJson(QJsonDocument::Compact));
}

// 模板函数：从 JSON 字符串反序列化
template<typename T>
T desFromJSONString(const QString& jsonString, T (*desFunc)(const QJsonObject&)) {
    QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString.toUtf8());
    if (!jsonDoc.isObject()) {
        qWarning() << "Invalid JSON string for deserialization:" << jsonString;
        return T();
    }
    return desFunc(jsonDoc.object());
}


template<typename EnumType>
QString sToJSONStringEnum(const EnumType& value, QJsonValue (*sFunc)(const EnumType&)) {
    QJsonObject jsonObject;
    jsonObject["value"] = sFunc(value);  // Use the macro-generated serialization function
    QJsonDocument jsonDoc(jsonObject);
    return QString::fromUtf8(jsonDoc.toJson(QJsonDocument::Compact));
}

// Deserialization function
template<typename EnumType>
EnumType desFromJSONStringEnum(const QString& jsonString, EnumType (*desFunc)(const QJsonValue&)) {
    QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString.toUtf8());
    if (!jsonDoc.isObject()) {
        qWarning() << "Invalid JSON string for deserialization:" << jsonString;
        return static_cast<EnumType>(0);  // Return default value
    }

    QJsonObject jsonObject = jsonDoc.object();
    if (!jsonObject.contains("value")) {
        qWarning() << "JSON object does not contain 'value' key:" << jsonString;
        return static_cast<EnumType>(0);  // Return default value
    }

    return desFunc(jsonObject["value"]);  // Use the macro-generated deserialization function
}

// 为 RTCEngineRequest 提供 QDataStream 操作符重载
static QDataStream& operator<<(QDataStream& out, const RTCEngineRequest& obj) {
    out << static_cast<int>(obj.command); // 假设 RTCEngineCommand 是枚举，序列化为 int
    out << obj.parameters;               // QVector<QVariant> 可以直接序列化
    return out;
}

static QDataStream& operator>>(QDataStream& in, RTCEngineRequest& obj) {
    int command;
    in >> command;                       // 反序列化为 int
    obj.command = static_cast<RTCEngineCommand::Engine_API>(command); // 转回枚举
    in >> obj.parameters;                // QVector<QVariant> 反序列化
    return in;
}

// 为 RTCEngineResponse 提供 QDataStream 操作符重载
static QDataStream& operator<<(QDataStream& out, const RTCEngineResponse& obj) {
    out << static_cast<int>(obj.command); // 假设 RTCEngineCommand 是枚举，序列化为 int
    out << obj.results;                   // QVector<QVariant> 可以直接序列化
    out << obj.errorMessage;              // QString 可以直接序列化
    return out;
}

static QDataStream& operator>>(QDataStream& in, RTCEngineResponse& obj) {
    int command;
    in >> command;                         // 反序列化为 int
    obj.command = static_cast<RTCEngineCommand::Engine_API>(command); // 转回枚举
    in >> obj.results;                     // QVector<QVariant> 反序列化
    in >> obj.errorMessage;                // QString 反序列化
    return in;
}

// 为 RTCEngineCallback 提供 QDataStream 操作符重载
static QDataStream& operator<<(QDataStream& out, const RTCEngineCallback& obj) {
    out << static_cast<int>(obj.event); // 假设 AliRtcCallbackEvent 是枚举，序列化为 int
    out << obj.results;                   // QVector<QVariant> 可以直接序列化
    return out;
}

static QDataStream& operator>>(QDataStream& in, RTCEngineCallback& obj) {
    int command;
    in >> command;                         // 反序列化为 int
    obj.event = static_cast<AliRtcEvents::AliRtcCallbackEvent>(command); // 转回枚举
    in >> obj.results;                     // QVector<QVariant> 反序列化
    return in;
}


#endif // SERIALIZATION_H
